#!/bin/bash

# 此脚本需要prometheus节点执行，包括检测prom-configserver进程状态、端口状态

# 系统ID
system_id=$(cat config.cnf | grep SystemId | awk -F "=" '{print $2}')
# 告警存放文件夹
alert_dir=$(cat config.cnf | grep AlertDir | awk -F "=" '{print $2}')
# 告警缓存文件
alarm_file_tmp="prom_configserver_check_alarm.txt"
echo -n "" >${alarm_file_tmp}
# 本机ip
local_ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
# prometheus端口
prom_configserver_port=$(cat config.cnf |grep PromconfigServerPort |awk -F "=" '{print $2}')
execute_cycle=$(cat config.cnf | grep ExecuteCycle | awk -F "=" '{print $2}')

# prometheus configserver进程状态监控
prom_configserver_pro_check() {

    prometheus_pro=$(ps -elf | grep "promtool configServer" | grep -v grep | wc -l)
	if [[ ${prometheus_pro} == 0 ]]; then
        # 进程状态不存在，告警
         ./gen_alarm.sh -g "应用进程监控" "prom-configserver服务运行状态异常,prom-configserver进程不存在,IP:$(hostname -I)" 3 "prom-configserver进程" "prom_configserver_pro" ${alarm_file_tmp}
    else
        # 告警存在，恢复原告警
        ./gen_alarm.sh -r "应用进程监控" "prom-configserver进程恢复正常,IP:$(hostname -I)" "prom-configserver进程" "prom_configserver_pro" ${alarm_file_tmp}

        # 端口检查
        prometheus_port_check
    fi
}

# prometheus configserver运行端口监控
prom_configserver_port_check() {

    port=$(netstat -nplt | grep ":${prom_configserver_port} ")
    if [[ ${port} == '' ]]; then
        # 端口未监听，告警
        ./gen_alarm.sh -g "端口状态" "prom-configserver服务端口[${prometheus_port}]未监听,IP:$(hostname -I)" 3 "prom-configserver端口状态" "prom_configserver_port" ${alarm_file_tmp}
    else
        # 端口正常且存在告警，需要恢复告警
        ./gen_alarm.sh -r "端口状态" "prom-configserver服务端口[${prometheus_port}]恢复正常,IP:$(hostname -I)" "prom-configserver端口状态" "prom_configserver_port" ${alarm_file_tmp}
    fi
}

while true; do
    holidays_date=$(cat config.cnf | grep Holidays | awk -F "=" '{print $2}' | sed "s/,/ /g")
	current_date=$(date +'%Y-%m-%d')
	echo $holidays_date | grep -o $current_date >> /dev/null
	if [[ $? -eq 0 ]]; then
		exit 0
	else
		echo -n "" >${alarm_file_tmp}
		prom_configserver_pro_check

		# 将缓存告警放入告警文件夹
		./gen_alarm.sh -s ${alarm_file_tmp}
		sleep ${execute_cycle}
	fi
done